<article id="wikiArticle">
<div></div>
<h2 id="Summary" name="Summary">概述</h2>
<p><code><strong>Math.hypot()</strong></code> 函数返回它的所有参数的平方和的平方根，即：</p>
<p><math display="block"><semantics><mrow><mstyle mathvariant="monospace"><mrow><mo lspace="0em" rspace="thinmathspace">Math.hypot</mo><mo stretchy="false">(</mo><msub><mi>v</mi><mn>1</mn></msub><mo>,</mo><msub><mi>v</mi><mn>2</mn></msub><mo>,</mo><mo>…</mo><mo>,</mo><msub><mi>v</mi><mi>n</mi></msub><mo stretchy="false">)</mo></mrow></mstyle><mo>=</mo><msqrt><mrow><munderover><mo>∑</mo><mrow><mi>i</mi><mo>=</mo><mn>1</mn></mrow><mi>n</mi></munderover><msubsup><mi>v</mi><mi>i</mi><mn>2</mn></msubsup></mrow></msqrt><mo>=</mo><msqrt><mrow><msubsup><mi>v</mi><mn>1</mn><mn>2</mn></msubsup><mo>+</mo><msubsup><mi>v</mi><mn>2</mn><mn>2</mn></msubsup><mo>+</mo><mo>…</mo><mo>+</mo><msubsup><mi>v</mi><mi>n</mi><mn>2</mn></msubsup></mrow></msqrt></mrow><annotation encoding="TeX">\mathtt{\operatorname{Math.hypot}(v_1, v_2, \dots, v_n)} = \sqrt{\sum_{i=1}^n v_i^2} = \sqrt{v_1^2 + v_2^2 + \dots + v_n^2}</annotation></semantics></math></p>
<h2 id="Syntax" name="Syntax">语法</h2>
<pre><code class="language-javascript"><code>Math.hypot([<em>value1</em>[,<em>value2</em>, ...]]) </code></code></pre>
<h2 id="Parameters" name="Parameters">参数</h2>
<dl>
<dt><code>value1, value2, ...</code></dt>
<dd>任意多个数字</dd>
</dl>
<h2 id="Description" name="Description">描述</h2>
<p>由于 <code>hypot</code> 是 <code>Math</code> 的静态方法，所以应该像这样使用：<code>Math.hypot()</code>，而不是作为你创建的 <code>Math</code> 实例的属性（<code>Math</code> 不是一个构造函数）。</p>
<p>如果不传入任何参数, 则返回 +0 .</p>
<p>如果参数列表中有至少一个参数不能被转换为数字，则返回 <a href="Reference/Global_Objects/NaN" title="全局属性 NaN 的值表示不是一个数字（Not-A-Number）。"><code>NaN</code></a>.</p>
<p>如果只传入一个参数, 则 <code>Math.hypot(x)</code> 的效果等同于 <code>Math.abs(x)</code>.</p>
<h2 id="Examples" name="Examples">示例</h2>
<pre><code class="language-js">Math.hypot(3, 4)        // 5
Math.hypot(3, 4, 5)     // 7.0710678118654755
Math.hypot()            // 0
Math.hypot(NaN)         // NaN
Math.hypot(3, 4, "foo") // NaN, +"foo" =&gt; NaN
Math.hypot(3, 4, "5")   // 7.0710678118654755, +"5" =&gt; 5
Math.hypot(-3)          // 3, the same as Math.abs(-3)
</code></pre>
<h2 id="Polyfill">Polyfill</h2>
<p>此函数可以使用如下代码模拟：</p>
<pre><code class="language-js language-js"><code class="language-js"><span class="keyword token">if</span> <span class="punctuation token">(</span><span class="operator token">!</span>Math<span class="punctuation token">.</span>hypot<span class="punctuation token">)</span> <span class="punctuation token">{</span>
  Math<span class="punctuation token">.</span>hypot <span class="operator token">=</span> <span class="keyword token">function</span> <span class="function token">hypot<span class="punctuation token">(</span></span><span class="punctuation token">)</span> <span class="punctuation token">{</span>
    <span class="keyword token">var</span> y <span class="operator token">=</span> <span class="number token">0</span><span class="punctuation token">;</span>
    <span class="keyword token">var</span> length <span class="operator token">=</span> arguments<span class="punctuation token">.</span>length<span class="punctuation token">;</span>

    <span class="keyword token">for</span> <span class="punctuation token">(</span><span class="keyword token">var</span> i <span class="operator token">=</span> <span class="number token">0</span><span class="punctuation token">;</span> i <span class="operator token">&lt;</span> length<span class="punctuation token">;</span> i<span class="operator token">++</span><span class="punctuation token">)</span> <span class="punctuation token">{</span>
      <span class="keyword token">if</span><span class="punctuation token">(</span>arguments<span class="punctuation token">[</span>i<span class="punctuation token">]</span> <span class="operator token">===</span> <span class="number token">Infinity</span> <span class="operator token">||</span> arguments<span class="punctuation token">[</span>i<span class="punctuation token">]</span> <span class="operator token">===</span> <span class="operator token">-</span><span class="number token">Infinity</span><span class="punctuation token">)</span> <span class="punctuation token">{</span>
        <span class="keyword token">return</span> <span class="number token">Infinity</span><span class="punctuation token">;</span>
      <span class="punctuation token">}</span>
      y <span class="operator token">+</span><span class="operator token">=</span> arguments<span class="punctuation token">[</span>i<span class="punctuation token">]</span> <span class="operator token">*</span> arguments<span class="punctuation token">[</span>i<span class="punctuation token">]</span><span class="punctuation token">;</span>
    <span class="punctuation token">}</span>
    <span class="keyword token">return</span> Math<span class="punctuation token">.</span><span class="function token">sqrt<span class="punctuation token">(</span></span>y<span class="punctuation token">)</span><span class="punctuation token">;</span>
  <span class="punctuation token">}</span><span class="punctuation token">;</span>
<span class="punctuation token">}</span></code></code></pre>
<h2 id="规范">规范</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">规范链接</th>
<th scope="col">规范状态</th>
<th scope="col">备注</th>
</tr>
<tr>
<td><a class="external" href="https://www.ecma-international.org/ecma-262/6.0/#sec-math.hypot" hreflang="en" lang="en" rel="noopener">ECMAScript 2015 (6th Edition, ECMA-262)<br/><small lang="zh-CN">Math.hypot</small></a></td>
<td><span class="spec-Standard">Standard</span></td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<div><div class="blockIndicator warning"><strong><a class="external" href="https://github.com/mdn/browser-compat-data" rel="noopener">We're converting our compatibility data into a machine-readable JSON format</a></strong>.
            This compatibility table still uses the old format,
            because we haven't yet converted the data it contains.
            <strong><a class="new" href="/zh-CN/docs/MDN/Contribute/Structures/Compatibility_tables" rel="nofollow">Find out how you can help!</a></strong></div>
<div class="htab">
<a id="AutoCompatibilityTable" name="AutoCompatibilityTable"></a>
<ul>
<li class="selected"><a>Desktop</a></li>
<li><a>Mobile</a></li>
</ul>
</div></div>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Chrome</th>
<th>Firefox (Gecko)</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari (WebKit)</th>
</tr>
<tr>
<td>Basic support</td>
<td>38</td>
<td><a href="/en-US/Firefox/Releases/27" title="Released on 2014-02-04.">27</a> (27)</td>
<td><span style="color: #f00;">未实现</span></td>
<td>25</td>
<td>7.1</td>
</tr>
</tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Android</th>
<th>Firefox Mobile (Gecko)</th>
<th>IE Phone</th>
<th>Opera Mobile</th>
<th>Safari Mobile</th>
</tr>
<tr>
<td>Basic support</td>
<td><span style="color: #f00;">未实现</span></td>
<td>27.0 (27)</td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #f00;">未实现</span></td>
</tr>
</tbody>
</table>
</div>
<h2 id="See_also" name="See_also">相关链接</h2>
<ul>
<li><a href="Reference/Global_Objects/Math" title="Math 是一个内置对象， 它具有数学常数和函数的属性和方法。不是一个函数对象。"><code>Math</code></a> 对象.</li>
</ul>
</article>